﻿using log4net;
using log4net.Config;
using log4net.Repository;
using Net5CommonLib.SystemLib.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Net5CommonLib.SystemLib.Tools.LogTools
{
    public class Log4Tools
    {
        private ILog _errorLog;
        private ILog _innorInfoLog;
        public ILog ErrorLog
        {
            get { return _errorLog; }
        }
        public ILog InnorInfoLog
        {
            get { return _innorInfoLog; }
        }
        public Log4Tools()
        {
            //创建日志仓储
            ILoggerRepository repository = LogManager.CreateRepository("CoreLogRepository");
            //读取日志配置文件
            XmlConfigurator.Configure(repository, new FileInfo("log4net.config"));
            //初始化错误日志操作对象
            _errorLog = LogManager.GetLogger(repository.Name, "errLog");
            //信息日志
            _innorInfoLog = LogManager.GetLogger(repository.Name, "infoLog");
        }
        /// <summary>
        /// 记录错误之日的方法
        /// </summary>
        /// <param name="throwMsg"></param>
        /// <param name="ex"></param>
        public void Error(string throwMsg, Exception ex)
        {
            string errorMsg = string.Format("【异常描述】：{0} \r\n【异常类型】：{1} \r\n【异常信息】：{2} \r\n【堆栈调用】：{3}",
               new object[] {
                    throwMsg,
                    ex.GetType().Name,
                    ex.Message,
                    ex.StackTrace });
            errorMsg = errorMsg.Replace("\r\n", "<br>");
            _errorLog.Error(errorMsg);
        }
        //记录信息日志的方法
        public void Info(string message)
        {
            _innorInfoLog.Info(string.Format("【日志信息】：{0}", message));
        }

        public static Log4Tools Log4ToolsObj
        {
            get { return DiManager.GetDiObj<Log4Tools>(); }
        }
    }
}
